﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using DTO;
using AutoMapper;

namespace DataAccess
{
    public static class PlayerTypeDAO
    {
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        static PlayerTypeDAO()
        {
            XmlConfigurator.Configure();
        }

        public static List<PlayerTypeDTO> GetPlayerTypeList()
        {
            try
            {
                List<PlayerTypeDTO> lstPlayerType = new List<PlayerTypeDTO>();

                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = from PlayerType in context.PLAYER_TYPE_LIST
                             select PlayerType;
                foreach (var item in query)
                {
                    PlayerTypeDTO dtoPlayerType = new PlayerTypeDTO();
                    dtoPlayerType = Mapper.Map<PLAYER_TYPE , PlayerTypeDTO>(item);
                    lstPlayerType.Add(dtoPlayerType);
                }
                return lstPlayerType;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return null;
            }
        }

        public static bool AddNewPlayerType(PlayerTypeDTO dtoPlayerType)
        {
            try
            {
                PLAYER_TYPE  obj = new PLAYER_TYPE ();
                //Mapping                
                obj = Mapper.Map<PlayerTypeDTO, PLAYER_TYPE >(dtoPlayerType);
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                context.AddToPLAYER_TYPE_LIST (obj);
                context.SaveChanges(true);

                dtoPlayerType.PLAYER_TYPE_ID  = obj.PLAYER_TYPE_ID ;
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool UpdatePlayerType(PlayerTypeDTO dtoPlayerType)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();

                PLAYER_TYPE  PlayerType = context.PLAYER_TYPE_LIST .First(g => g.PLAYER_TYPE_ID  == dtoPlayerType.PLAYER_TYPE_ID );                
                PlayerType.PLAYER_TYPE_DESC  = dtoPlayerType.PLAYER_TYPE_DESC ;
                context.SaveChanges(true);
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteAPlayerType(PlayerTypeDTO dtoPlayerType)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                PLAYER_TYPE PlayerType = context.PLAYER_TYPE_LIST.First(g => g.PLAYER_TYPE_ID == dtoPlayerType.PLAYER_TYPE_ID);
                context.DeleteObject(PlayerType);
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteAllPlayerType()
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = (from PlayerType in context.PLAYER_TYPE_LIST
                             select PlayerType);
                foreach (var item in query)
                {
                    context.DeleteObject(item);
                }
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

    }
}
